import { defineStore } from 'pinia'

interface ITabList {
  title: string
  path: string
}

export const useTabListStore = defineStore(
  'tabList',
  () => {
    const tabList = ref<ITabList[]>([{ title: '首页', path: '/' }])

    const addTab = (tab: ITabList) => {
      tabList.value.findIndex((item) => item.path === tab.path) === -1 && tabList.value.push(tab)
    }

    const removeTabIndex = (index: number) => {
      tabList.value.length > index && tabList.value.splice(index, 1)
    }

    const removeOtherTabs = (name: string) => {
      tabList.value = tabList.value.filter((tab) => tab.path === name || tab.path === '/')
    }

    const removeAllTabs = () => {
      tabList.value = [{ title: '首页', path: '/' }]
    }

    return {
      tabList,
      addTab,
      removeTabIndex,
      removeOtherTabs,
      removeAllTabs
    }
  },
  {
    persist: true // 启用持久化存储，这样刷新页面后用户信息不会丢失
  }
)
